<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close" :before-close="beforeClose" :append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="交付物编码" prop="code">
            <el-input v-model="model.code" readonly class="is-disabled" style="width:93%"></el-input>
            <tl-help ref="help">新增时自动生成 </tl-help>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交付物名称" prop="name" :rules="rules.validateRequire">
            <el-input v-model="model.name" maxlength="100" show-word-limit clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交付物类型" prop="deliverabletype" :rules="rules.validateRequireSelect">
            <el-select v-model="model.deliverabletype" filterable clearable>
              <el-option v-for="item in codeModel.deliverabletypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
					<el-form-item label="是否公开" prop="open" :rules="rules.validateRequireSelect">
						<el-select v-model="model.open" filterable clearable>
							<el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
						</el-select>
					</el-form-item>
				</el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="4" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" :before-close="beforeClose">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">保存</el-button>
      <tl-attachment ref="attachmentRef" :sourceType="getSourceType" :sourceId="this.model.sourceid" :disabled="disableAttachment" :editable="stateEdit" :previewMode="false" btnTitle="交付物"></tl-attachment>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    return {
      moduleTitle: '项目交付物',
      urlPrefix: '/cbb/pmDeliverable/',
      url: {
        // 额外的url
      },
      addRules: {
        validateRequireSelect: [
          // 必选
          { required: 'true', message: '请选择内容', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    /** 项目号 */
    projectcode: {
      type: String
    }
  },
  computed: {
    // 新增
    stateAdd() {
      return !this.model.id
    },
    // 禁用附件按钮
    disableAttachment() {
      return this.stateAdd
    },
    // 编辑
    stateEdit() {
      return this.model.id && !this.disableEditSubmit
    },
    // 来源单据类型
    getSourceType() {
      return this.model.sourcetype ? this.model.sourcetype : 'PmDeliverable'
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, 'open', 1)
        this.$set(this.model, 'projectcode', this.projectcode)
      }
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    /** 执行编辑提交 */
    doEdit() {
      this.saveLoading = true
      putAction(this.url.edit, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message(res.message, 'success')
            this.queryData(this.model.id)
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    /** 取消保存 */
    doCancelEdit() {
      let fileNum = this.$refs.attachmentRef.fileList.length > 0 ? this.$refs.attachmentRef.fileList.length : this.$refs.attachmentRef.fileCount
      if (fileNum === 0 && this.model.sourcemodule === '01') {
        this.$tap.alert('请上传交付物！', 'warning')
        return
      }
      this.visible = false
    },
    // 关闭前判断是否上传了附件
    beforeClose(done) {
      let fileNum = this.$refs.attachmentRef.fileList.length > 0 ? this.$refs.attachmentRef.fileList.length : this.$refs.attachmentRef.fileCount
      if (fileNum === 0 && this.model.sourcemodule === '01') {
        this.$tap.alert('请上传交付物！', 'warning')
        return
      } else {
        done()
      }
    },
    close() {
      this.$emit('refresh')
    }
  }
}
</script>
